<?php
/**
 * Created by PhpStorm.
 * User: moquan
 * Date: 2018/6/21
 * Time: 9:22
 */

namespace app\admin\model;


use app\admin\validate\UserVali;
use app\lib\exception\AdminException;
use app\lib\exception\ParamException;
use think\Request;

class AdminModel extends BaseModel {
    /*
     * 登陆
     *
     * */
    public static function doLogin(){
        if( Request::instance()->isPost() ){
            $validate = new UserVali();
            $param = $validate->goCheck( 'login' );
            $param['password'] = md5( $param['password'] );
            $result = AdminUser::getAdminUserByUserPwd( $param );

            if( $result ){
                session('admin',$result->toArray());
                session('admin.admin_id',$result->admin_id);
                return true;
            }else{
                throw new AdminException();
            }
        }
        return false;
    }


    /*
     * 检查是否需要登陆
     * return boolean
     * */
    public static function isMustLogin(){
        $module = Request::instance()->module();
        $controller = Request::instance()->controller();
        $action = Request::instance()->action();
        $adminID = session('?admin');
        //在指定控制器(登陆、测试)
        if ( in_array( strtolower( $controller ), ['login', 'test'] ) || ( strtolower( $controller ) == 'admin' && strtolower( $action )  == 'login' )) {
            return false;
        }else{
            //是否已经登陆 (session 中是否存在admin_id)
            if( !$adminID ){
                return true;
            }else{
                return false;
            }
        }

    }

}